//
//  GameChargeViewController.m
//  EasyToo
//
//  Created by YuanDee on 13-8-24.
//
//

#import "GameChargeViewController.h"
#import "GameChargeConfirmViewController.h"

@interface GameChargeViewController ()

@end

@implementation GameChargeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    UILabel *titleLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(30, 20, 250, 30)];
    [titleLabel1 setText:@"选择游戏和金额"];
    [self.view addSubview:titleLabel1];
    
    
    
    self.gameName = [[UITextField alloc] initWithFrame:CGRectMake(30, 60, 250, 30)];
    [self.gameName setBorderStyle:UITextBorderStyleBezel];
    [self.view addSubview:self.gameName];
    self.gameName.delegate = self;
    self.gameName.tag = 1;
    
    // 建立 UIToolbar
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(cancelPicker)];
    // 把按鈕加進 UIToolbar
    toolBar.items = [NSArray arrayWithObject:right];
    self.gameName.inputAccessoryView = toolBar;
    
    pickerArray = [[NSArray arrayWithObjects:@"10",@"30",@"50",@"100",@"200",@"250", nil] retain];
    selectPicker = [[UIPickerView alloc]initWithFrame:CGRectZero];
    selectPicker.showsSelectionIndicator = YES;
    selectPicker.frame = CGRectMake(0, 100, 0, 0);
    selectPicker.delegate = self;
    selectPicker.dataSource = self;
    
    self.gameName.inputAccessoryView = toolBar;
    
    
    selectPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    self.gameName.inputView = selectPicker;
    
    self.chargePrice = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 250, 30)];
    [self.chargePrice setBorderStyle:UITextBorderStyleBezel];
    [self.view addSubview:self.chargePrice];
    self.chargePrice.delegate = self;
    self.chargePrice.tag = 2;
    self.chargePrice.inputAccessoryView = toolBar;
    selectPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    self.chargePrice.inputView = selectPicker;
    
    /*
     self.cardNum = [[UITextField alloc] initWithFrame:CGRectMake(30, 150, 250, 30)];
     [self.cardNum setBorderStyle:UITextBorderStyleBezel];
     [self.view addSubview:self.cardNum];
     self.cardNum.delegate = self;
     self.cardNum.tag = 3;
     self.cardNum.inputAccessoryView = toolBar;
     selectPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
     self.cardNum.inputView = selectPicker;
     */
    
    UILabel *titleLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(30, 150, 250, 30)];
    [titleLabel2 setText:@"填写帐号信息"];
    [self.view addSubview:titleLabel2];
    
    self.account = [[UITextField alloc] initWithFrame:CGRectMake(30, 190, 250, 30)];
    [self.account setBorderStyle:UITextBorderStyleBezel];
    self.account.placeholder = @"充值帐户";
    [self.view addSubview:self.account];
    self.account.delegate = self;
    self.account.tag = 3;
    //self.account.inputAccessoryView = toolBar;
    selectPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    //    self.account.inputView = selectPicker;
    
    /*
     self.account2 = [[UITextField alloc] initWithFrame:CGRectMake(30, 230, 250, 30)];
     [self.account2 setBorderStyle:UITextBorderStyleBezel];
     self.account2.placeholder = @"确认充值帐号";
     [self.view addSubview:self.account2];
     self.account2.delegate = self;
     self.account2.tag = 5;
     self.account2.inputAccessoryView = toolBar;
     selectPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
     self.account2.inputView = selectPicker;
     */
    
    UILabel *titleLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(30, 240, 250, 30)];
    [titleLabel3 setText:@"选择支付方式"];
    [self.view addSubview:titleLabel3];
    
    self.zfbButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 280, 100, 30)];
    //[self.zfbButton setBackgroundImage:[UIImage imageNamed:@"zfb"] forState:UIControlStateNormal];
    [self.zfbButton addTarget:self action:@selector(zfbButtonPick:) forControlEvents:UIControlEventTouchDown];
    [self.zfbButton setBackgroundImage:[UIImage imageNamed:@"zfb.png"] forState:UIControlStateNormal];
    [self.zfbButton setBackgroundImage:[UIImage imageNamed:@"zhifubao2.png"] forState:UIControlStateSelected];
    [self.view addSubview:self.zfbButton];
    
    self.ybButton = [[UIButton alloc] initWithFrame:CGRectMake(180, 280, 100, 30)];
    //[self.ybButton setBackgroundImage:[UIImage imageNamed:@"yinlian1"] forState:UIControlStateNormal];
    [self.ybButton addTarget:self action:@selector(ybButtonPick:) forControlEvents:UIControlEventTouchDown];
    [self.ybButton setBackgroundImage:[UIImage imageNamed:@"yinlian1.png"] forState:UIControlStateNormal];
    [self.ybButton setBackgroundImage:[UIImage imageNamed:@"yinlian2.png"] forState:UIControlStateSelected];
    [self.view addSubview:self.ybButton];
    
    UIButton *nextStep =[UIButton buttonWithType:UIButtonTypeCustom];
    nextStep.frame = CGRectMake(40, 350, 230, 30);
    [nextStep setBackgroundImage:[UIImage imageNamed:@"next1"] forState:UIControlStateNormal];
    [nextStep setTitle:@"下一步" forState:UIControlStateNormal];
    [self.view addSubview:nextStep];
    [nextStep addTarget:self action:@selector(confirmMessge:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
    self.view.frame = rect;
    [UIView commitAnimations];
    [textField resignFirstResponder];
    return YES;
}

-(void)confirmMessge:(id)sender
{
    /*
    if( [self.account.text isEqualToString:self.account.text] == NO)
    {
        UIAlertView *av= [[UIAlertView alloc] initWithTitle:@"提示" message:@"请确认帐号" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [av show];
    }
    else*/
    {
        GameChargeConfirmViewController  *confirmView = [[GameChargeConfirmViewController alloc] init];
        [self.navigationController pushViewController:confirmView animated:YES];
    }
     
}


#pragma picker delegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [pickerArray count];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [pickerArray objectAtIndex:row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:UITextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    float width = self.view.frame.size.width;
    float height = self.view.frame.size.height;
    if(offset > 0)
    {
        CGRect rect = CGRectMake(0.0f, -offset,width,height);
        self.view.frame = rect;
    }
    [UIView commitAnimations];
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSInteger row = [selectPicker selectedRowInComponent:0];
    printf("textField tag is:%d\n",textField.tag);
    if( textField.tag == 1)
        self.gameName.text = [pickerArray objectAtIndex:row];
    if( textField.tag == 2)
        self.chargePrice.text = [pickerArray objectAtIndex:row];
    if( textField.tag == 3)
        self.account.text = [pickerArray objectAtIndex:row];
    /*
     if( textField.tag == 4)
     self.account.text = [pickerArray objectAtIndex:row];
     if( textField.tag == 5)
     self.account2.text = [pickerArray objectAtIndex:row];
     */
}

// 按下完成鈕後的 method
-(void) cancelPicker {
    [self.gameName endEditing:YES];
    [self.chargePrice endEditing:YES];
    [self.account endEditing:YES];
    //[self.account endEditing:YES];
    //[self.account2 endEditing:YES];
}

//支付方式
-(void)zfbButtonPick:(id)sender
{
    [self.zfbButton setSelected:YES];
    [self.ybButton setSelected:NO];
}
    


-(void)ybButtonPick:(id)sender
{
    [self.zfbButton setSelected:NO];
    [self.ybButton setSelected:YES];
}
@end
